package com.cssl.Controller;

import com.cssl.Until.RedisUtil;
import com.cssl.entity.Users;
import com.cssl.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.regex.Pattern;

@RestController
@ResponseBody
public class UsersController {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private UsersService usersService;
    @Autowired
    private RedisUtil redisUtil;
    @RequestMapping("add")
    public String Add(Users users){
        redisUtil.add(users.getName(),users);
        return "成功";
    }
    @RequestMapping("delete/{name}")
    public String delete(@PathVariable("name") String name){
        redisUtil.remove(name);
        return "成功";
    }
    @RequestMapping("update")
    public String update(Users users){
        redisUtil.set(users.getName(),users);
        return "成功";
    }
    @RequestMapping("find/{name}")
    public Users find(@PathVariable("name") String name){
        Users us=(Users) redisUtil.get(name);
        return us;
    }
    @RequestMapping("/Refind/{name}")
    @Cacheable(value="Users", key="#id", unless="#result==null")
    public Users Refind(@PathVariable("name") String name){
        return (Users) redisUtil.get(name);
    }
    @RequestMapping("/Redelete/{name}")
    @CacheEvict(value="Users", key="#name")
    public String Redelete(@PathVariable("name") String name){
        redisUtil.remove(name);
        return "ok";
    }
    @CachePut(value="Users", key="#name")
    @RequestMapping("/Reupdate/{name}")
    public String Reupdate(@PathVariable("name") String id,Users users){
        redisUtil.set(users.getName(),users);
        return  "ok";
    }

    /**
     * ====mongodb======
     */
    @GetMapping("/save")
    public Users save(Users users){
        return mongoTemplate.save(users);
        //return usersService.save(users);
    }
    @GetMapping("/findAll")
    public List<Users> findAll(){
        return  mongoTemplate.findAll(Users.class);
        //return usersService.findAll();
    }
    @GetMapping("/findByname")
    public List<Users> findByname(String name){
        Query query=new Query(Criteria.where("name").is(name));
        return mongoTemplate.find(query,Users.class);
        //return usersService.findByname(name);
    }
    @GetMapping("/findByUsernameAndPassword")
    public List<Users> findByUsernameAndPassword(String name,int pwd){
        Criteria criteria=Criteria.where("name").is(name).where("pwd").is(pwd);
        Query query=new Query(criteria);
        return mongoTemplate.find(query,Users.class);
        //return usersService.findBynameAndPassword(name,pwd);
    }
    @GetMapping("/update")
    public Long update(String name,String newName){
        Query query=new Query(Criteria.where("name").is(name));
        Update update=new Update().set("name",newName);
        return mongoTemplate.updateMulti(query,update,Users.class).getModifiedCount();
    }
    /**
     * 模糊查询+排序分页
     * @return
     */
    @GetMapping("/findByPage")
    public List<Users> findByPage(String name, Integer pageIndex, Integer pageSize){
        Query query = new Query();
        //模糊查询
        Criteria criteria = new Criteria();
        Pattern pattern = Pattern.compile("^.*" + name + ".*$", Pattern.CASE_INSENSITIVE);
        criteria.andOperator(Criteria.where("name").regex(pattern));
        query.addCriteria(criteria);
        //排序
        query.with(Sort.by(Sort.Direction.DESC,"id"));
        //分页
        query.skip((pageIndex - 1) * pageSize);                 // 从那条记录开始
        query.limit(pageSize);                                  // 取多少条记录
        long count = mongoTemplate.count(query, Users.class);  	// 总记录数
        System.out.println("count:"+count);
        return mongoTemplate.find(query, Users.class);
    }
}
